var prefix = "/sys/set"
$(document).on('click','.switchOn',function(){
    $(this).toggleClass('switchOff');
    var switchOn=$(this).attr("class")
    var setcode=$(this).attr("id")
    //开
    if(switchOn.indexOf("switchOff")==-1){
        $.ajax({
            url : prefix + "/update",
            type : "post",
            data : {
                'setCode' : setcode,
                'setValue': 1
            },
            success : function(r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    }else{
        $.ajax({
            url : prefix + "/update",
            type : "post",
            data : {
                'setCode' : setcode,
                'setValue': 0
            },
            success : function(r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    }
});

function changeUserType(obj) {
    $.ajax({
        url : prefix + "/update",
        type : "post",
        data : {
            'setCode' : "noUserTypeOn",
            'setValue': obj
        },
        success : function(r) {
            if (r.code == 0) {
                layer.msg(r.msg);
                reLoad();
            } else {
                layer.msg(r.msg);
            }
        }
    });
}


$(function(){
    load();
});

function reLoad() {
    load();
}

function load() {
    $.ajax({
        url : prefix + "/list",
        type : "post",
        success : function(r) {
            if (r.code == 0) {
                var synchroIp=r.map.synchroIp
                var synchroPort=r.map.synchroPort
                var version=r.map.version
                $("#synchroIp").val(synchroIp)
                $("#synchroPort").val(synchroPort)
                $("#version").val(version)
                var setDOList=r.map.setDOList
                $(".text_show").remove()
                $(".forbid_show").remove()
                $(".Keyword_show").remove()
                for (var i=0;i<setDOList.length;i++){
                    if(setDOList[i].setCode=="enableFileAuidtOn"){
                        if(setDOList[i].setValue==0){
                            $("#enableFileAuidtOn").attr("class","switchOn switchOff")
                            $("#noAuidt").css('display','none');
                        }else{
                            $("#enableFileAuidtOn").attr("class","switchOn")
                            $("#noAuidt").css('display','block');
                        }
                    }
                    if(setDOList[i].setCode=="noAuidtTypeOn"){
                        if(setDOList[i].setValue==0){
                            $("#noAuidtTypeOn").attr("class","switchOn switchOff")
                            $("#typeOn").css('display','none');
                        }else{
                            $("#noAuidtTypeOn").attr("class","switchOn")
                            $("#typeOn").css('display','block');
                        }
                    }
                    if(setDOList[i].setCode=="noUserTypeOn"){
                        if(setDOList[i].setValue==1){
                            $("input[name='noUserTypeOn'][value='1']").attr("checked",true);
                        }else if(setDOList[i].setValue==2){
                            $("input[name='noUserTypeOn'][value='2']").attr("checked",true);
                        }else if(setDOList[i].setValue==3){
                            $("input[name='noUserTypeOn'][value='3']").attr("checked",true);
                        }
                    }
                    if(setDOList[i].setCode=="noAuidtFile"){
                        $(".type_pp").append("<p class=\"text_show\"><span class=\"panel-jian\" onclick=\"remove("+setDOList[i].id+","+"'"+setDOList[i].setValue+"')\">-</span><span>&nbsp;&nbsp;&nbsp;</span><span class=\"text\">"+setDOList[i].setValue+"</span></p>")
                    }
                    if(setDOList[i].setCode=="forbidFileType"){
                        if(setDOList[i].setValue==0){
                            $("#forbidFileType").attr("class","switchOn switchOff")
                            $("#forbid").css('display','none');
                        }else{
                            $("#forbidFileType").attr("class","switchOn")
                            $("#forbid").css('display','block');
                        }
                    }
                    if(setDOList[i].setCode=="forbidType"){
                        $(".type_forbid").append("<p class=\"forbid_show\"><span class=\"panel-jian\" onclick=\"remove("+setDOList[i].id+","+"'"+setDOList[i].setValue+"')\">-</span><span>&nbsp;&nbsp;&nbsp;</span><span class=\"text\">"+setDOList[i].setValue+"</span></p>")
                    }
                    if(setDOList[i].setCode=="uploadForbidType"){
                        $(".type_forbid_upload").append("<p class=\"forbid_show\"><span class=\"panel-jian\" onclick=\"remove("+setDOList[i].id+","+"'"+setDOList[i].setValue+"')\">-</span><span>&nbsp;&nbsp;&nbsp;</span><span class=\"text\">"+setDOList[i].setValue+"</span></p>")
                    }
                    if(setDOList[i].setCode=="forbidKeyword"){
                        if(setDOList[i].setValue==0){
                            $("#forbidKeyword").attr("class","switchOn switchOff")
                            $("#forbidKeywordFile").css('display','none');
                        }else{
                            $("#forbidKeyword").attr("class","switchOn")
                            $("#forbidKeywordFile").css('display','block');
                        }
                    }
                    if(setDOList[i].setCode=="Keyword"){
                        $(".type_Keyword").append("<p class=\"Keyword_show\"><span class=\"panel-jian\" onclick=\"remove("+setDOList[i].id+","+"'"+setDOList[i].setValue+"')\">-</span><span>&nbsp;&nbsp;&nbsp;</span><span class=\"text\">"+setDOList[i].setValue+"</span></p>")
                    }

                    if(setDOList[i].setCode=="ip"){
                        $("#ip").val(setDOList[i].setValue)
                    }

                    if(setDOList[i].setCode=="port"){
                        $("#port").val(setDOList[i].setValue)
                    }

                    if(setDOList[i].setCode=="diskSave"){
                        $("#diskSave").val(setDOList[i].setValue)
                    }
                }
            }
        }
    });
}

function showInput(type) {
    layer.open({
        type : 2,
        title : '增加',
        maxmin : true,
        shadeClose : false, // 点击遮罩关闭层
        area : [ '400px', '320px' ],
        content :prefix + '/add/' + type
    });
}

function remove(id,setValue) {
    layer.confirm('确定要删除类型【'+setValue+'】？', {
        btn : [ '确定', '取消' ]
    }, function() {
        $.ajax({
            url : prefix + "/remove",
            type : "post",
            data : {
                'id' : id
            },
            success : function(r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}

function savePort() {
    var synchroIp = $("#synchroIp").val()
    var synchroPort = $("#synchroPort").val()
    $.ajax({
        url : prefix + "/updateProperties",
        type : "post",
        data : {
            'synchroIp' : synchroIp,
            'synchroPort': synchroPort
        },
        success : function(r) {
            if (r.code == 0) {
                layer.msg(r.msg);
                reLoad();
            } else {
                layer.msg(r.msg);
            }
        }
    });
}

function saveDiskSave() {
    var diskSave=$("#diskSave").val()
    $.ajax({
        url : prefix + "/update",
        type : "post",
        data : {
            'setCode' : 'diskSave',
            'setValue': diskSave
        },
        success : function(r) {
            if (r.code == 0) {
                layer.msg(r.msg);
                reLoad();
            } else {
                layer.msg(r.msg);
            }
        }
    });
}

